package com.shahe.GirlsDay.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.*;
import android.widget.*;

import com.shahe.GirlsDay.LogActivity;
import com.shahe.GirlsDay.R;
import com.shahe.GirlsDay.util.StaticClass;

public class BasicFragment extends Fragment {

	final static int WRAP_CONTENT = -2;
	int[] msgIds = { R.string.blank, R.string.log_b_mucus_creamy,
			R.string.log_b_mucus_dry, R.string.log_b_mucus_egg,
			R.string.log_b_mucus_sticky, R.string.log_b_mucus_watery };
	private Switch switchDayType;
	private RatingBar ratingBarFlow;
	private RadioGroup radioGroupColor;
	public static EditText tempEdit;
	public static EditText weightEdit;
	private Switch switchHadFun;
	private Switch switchTakePills;
	public static TextView firstDay;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		final View view = inflater.inflate(R.layout.log_basic, null);
		firstDay = (TextView) view.findViewById(R.id.tv_log_ba01);
		
		if (StaticClass.dayType == 2) {
			firstDay.setText("First day of period");
		} else if (StaticClass.dayType ==1) {
			firstDay.setText("Last day of period");
		}
		
		if(LogActivity.oneDayInfo.getDaytype().equals("1")){
			firstDay.setText("First day of period");
		}
		
		
		//getActivity().getWindow().setSoftInputMode(
				//WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		switchDayType = (Switch) view.findViewById(R.id.log_ba_sw_firstDay);
		//switchDayType.setSelected(true);
		if (LogActivity.oneDayInfo.getDaytype().equals("1")) {
			switchDayType.setChecked(true);
			firstDay.setText("First day of period");
		} else if (LogActivity.oneDayInfo.getDaytype().equals("2")) {
			firstDay.setText("Last day of period");
			switchDayType.setChecked(true);
		}
		switchDayType
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked
								&& firstDay.getText().equals(
										"First day of period")) {
							// Toast.makeText(getActivity(), "True"
							// ,Toast.LENGTH_SHORT).show();
							LogActivity.oneDayInfo.setDaytype("1");

						} else if (isChecked
								&& firstDay.getText().equals(
										"Last day of period")) {
							LogActivity.oneDayInfo.setDaytype("2");
						} else {
							// Toast.makeText(getActivity(), "False"
							// ,Toast.LENGTH_SHORT).show();
							LogActivity.oneDayInfo.setDaytype("0");
						}
					}
				});

		ratingBarFlow = (RatingBar) view.findViewById(R.id.log_ba_rat_flow);
		if(LogActivity.oneDayInfo.getFlow().equals("1")){
			ratingBarFlow.setRating((float) 1.0);
		}else if(LogActivity.oneDayInfo.getFlow().equals("2")){
			ratingBarFlow.setRating((float) 2.0);
		}else if(LogActivity.oneDayInfo.getFlow().equals("3")){
			ratingBarFlow.setRating((float) 3.0);
		}
		
		ratingBarFlow
				.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {

						String flow = ((int) ratingBarFlow.getRating()) + "";
						LogActivity.oneDayInfo.setFlow(flow);
						Toast.makeText(getActivity(), flow, Toast.LENGTH_SHORT).show();
					}
				});

		radioGroupColor = (RadioGroup) view.findViewById(R.id.log_ba_color);
		if(LogActivity.oneDayInfo.getColour().equals("1")){
			RadioButton radioButton = (RadioButton) view
					.findViewById(R.id.radioButton);
			radioButton.setChecked(true);
		}else if(LogActivity.oneDayInfo.getColour().equals("2")){
			RadioButton radioButton = (RadioButton) view
					.findViewById(R.id.radioButton3);
			radioButton.setChecked(true);
		}else if(LogActivity.oneDayInfo.getColour().equals("3")){
			RadioButton radioButton = (RadioButton) view
					.findViewById(R.id.radioButton2);
			radioButton.setChecked(true);
		}
		
		radioGroupColor
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						int radioButtonId = group.getCheckedRadioButtonId();
						RadioButton radioButton = (RadioButton) view
								.findViewById(radioButtonId);
						LogActivity.oneDayInfo.setColour(radioButton.getText()
								+ "");
						// Toast.makeText(getActivity(), radioButton.getText()
						// +"",Toast.LENGTH_SHORT).show();
					}
				});
		
		

		final Spinner spinner = (Spinner) view
				.findViewById(R.id.sp_log_b_mucus);
		BaseAdapter baseAdapter = new BaseAdapter() {
			@Override
			public int getCount() {
				return 6;
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				LinearLayout linearLayout = new LinearLayout(getActivity());
				linearLayout.setOrientation(LinearLayout.HORIZONTAL);
				// linearLayout.setBackgroundColor(getResources().getColor(R.color.title_color));

				TextView textView = new TextView(getActivity());
				textView.setText("" + getResources().getText(msgIds[position]));
				textView.setTextSize(22);
				// textView.setGravity(Gravity.BOTTOM);
				// textView.setBackgroundColor(getResources().getColor(R.color.title_color));
				textView.setTextColor(getResources().getColor(
						R.color.title_color));
				linearLayout.addView(textView);

				return linearLayout;
			}
		};
		spinner.setAdapter(baseAdapter);
		switch(LogActivity.oneDayInfo.getMucus()){
		case "Creamy": spinner.setSelection(1); break;
		case "Dry":  spinner.setSelection(2); break;
		case "Egg white": spinner.setSelection(3); break;
		case "Sticky": spinner.setSelection(4); break;
		case "Watery": spinner.setSelection(5); break;
		default: break;
		}
	
		spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (position != 0) {
					String mucus = spinner.getResources().getText(
							msgIds[position])
							+ "";
					LogActivity.oneDayInfo.setMucus(mucus);
				}

				// Toast.makeText(getActivity(), msgIds[position]
				// +"",Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}

		});

		tempEdit = (EditText) view.findViewById(R.id.et_log_ba_temp);
		if (!LogActivity.oneDayInfo.getTemperature().equals(" ")) {
			tempEdit.setText(LogActivity.oneDayInfo.getTemperature());
		}

		weightEdit = (EditText) view.findViewById(R.id.et_log_ba_wei);
		if (!LogActivity.oneDayInfo.getWeight().equals(" ")) {
			weightEdit.setText(LogActivity.oneDayInfo.getWeight());
		}

		switchHadFun = (Switch) view.findViewById(R.id.log_ba_sw_fun);
		if (LogActivity.oneDayInfo.getHadfun().equals("1")) {
			switchHadFun.setChecked(true);
		}
		switchHadFun
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							// Toast.makeText(getActivity(), "True"
							// ,Toast.LENGTH_SHORT).show();
							LogActivity.oneDayInfo.setHadfun("1");

						} else {
							// Toast.makeText(getActivity(), "False"
							// ,Toast.LENGTH_SHORT).show();
							LogActivity.oneDayInfo.setHadfun("0");
						}
					}
				});

		switchTakePills = (Switch) view.findViewById(R.id.log_ba_sw_pill);
		if (LogActivity.oneDayInfo.getTakepills().equals("1")) {
			switchTakePills.setChecked(true);
		}
		switchTakePills
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							// Toast.makeText(getActivity(), "True"
							// ,Toast.LENGTH_SHORT).show();
							LogActivity.oneDayInfo.setTakepills("1");

						} else {
							// Toast.makeText(getActivity(), "False"
							// ,Toast.LENGTH_SHORT).show();
							LogActivity.oneDayInfo.setTakepills("0");
						}
					}
				});
		return view;
	}
}
